package com.objecteffects.template.util.logger;

import java.lang.reflect.Field;

import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;

import org.slf4j.Logger;

public final class Slf4jTypeListener implements TypeListener {
    @Override
    public <I> void hear(final TypeLiteral<I> aTypeLiteral,
        final TypeEncounter<I> aTypeEncounter) {

        for (final Field field : aTypeLiteral.getRawType().getDeclaredFields()) {
            if (field.getType() == Logger.class &&
                field.isAnnotationPresent(InjectLogger.class)) {
                aTypeEncounter.register(new Slf4jMembersInjector<I>(field));
            }
        }
    }
}
